
global using SpriteInfo = voidbox.PackedTextureElement;
using System;
using System.Collections.Generic;
using System.Numerics;
using voidbox;

public class GameApp : voidbox.IUserApp
{
    IVoidBox voidBox;

    SpriteInfo logo;
    public void OnStart(IVoidBox voidBox)
    {
        this.voidBox = voidBox;
        voidBox.Debug.Log("log", "hello world.");
        logo = voidBox.Graphics.GetLogoSprite();

        for (var i = 0; i < 100; i++)
        {
            drawpos.Add(new DrawPos { pos = new Vector3(i, i, 0), wantPos = new Vector3(i, i, 0) });
        }
    }
    class DrawPos
    {
        public Vector3 pos;
        public Vector3 wantPos;
    }
    List<DrawPos> drawpos = new List<DrawPos>();
    Random random = new Random();
    public void OnRender()
    {
        voidBox.Graphics.Clear(new Color(1f, 1f, 0.5f, 1f));

        for (var i = 0; i < drawpos.Count; i++)
        {
            voidBox.Graphics.DrawSprite(logo, Color.White, drawpos[i].pos, new Vector2(1, 1), 0);
        }

        voidBox.Graphics.EndDraw();
    }
    
    public void OnUpdate(float delta)
    {
        foreach(var pos in drawpos)
        {
            var dist = Vector3.Distance(pos.pos, pos.wantPos);
            if (dist<2)
            {
                pos.wantPos.X = random.NextSingle() * 500;
                pos.wantPos.Y = random.NextSingle() * 500;
            }
            else
            {
                var dir =Vector3.Normalize(pos.wantPos-pos.pos);
                var movedist = 100 * delta;
                if(movedist>dist)
                    movedist = dist;

                pos.pos += dir * movedist;
            }
        }
    }
}